package com.ustropo.financas.navigator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ustropo.financas.R;

/**
 * 
 * @author Fernando C. de Souza
 * @version 1.0
 * @since 08/10/2013
 */
public class NavDrawerAdapter extends ArrayAdapter<NavDrawerItem> {

	/**
	 * Internal variable to the layout inflater
	 */
    private LayoutInflater inflater;
    
    /**
     * 
     * @param context
     * @param textViewResourceId
     * @param objects
     */
    public NavDrawerAdapter(Context context, int textViewResourceId, NavDrawerItem[] objects ) {
        super(context, textViewResourceId, objects);
        this.inflater = LayoutInflater.from(context);
    }

    /*
     * (non-Javadoc)
     * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null ;
        NavDrawerItem menuItem = this.getItem(position);
        if ( menuItem.getType() == NavMenuItem.ITEM_TYPE ) {
            view = getItemView(convertView, parent, menuItem );
        }
        else {
            view = getSectionView(convertView, parent, menuItem);
        }
        return view ;
    }
    
   /**
    * 
    * @param convertView
    * @param parentView
    * @param navDrawerItem
    * @return
    */
    public View getItemView( View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem ) {
        
        NavMenuItem menuItem = (NavMenuItem) navDrawerItem ;
        NavMenuItemHolder navMenuItemHolder = null;
        
        if (convertView == null) {
            convertView = inflater.inflate( R.layout.nav_drawer_item, parentView, false);
            TextView labelView = (TextView) convertView
                    .findViewById( R.id.nav_menuitem_label );
            ImageView iconView = (ImageView) convertView
                    .findViewById( R.id.nav_menuitem_icon );

            navMenuItemHolder = new NavMenuItemHolder();
            navMenuItemHolder.labelView = labelView ;
            navMenuItemHolder.iconView = iconView ;

            convertView.setTag(navMenuItemHolder);
        }

        if ( navMenuItemHolder == null ) {
            navMenuItemHolder = (NavMenuItemHolder) convertView.getTag();
        }
                    
        navMenuItemHolder.labelView.setText(menuItem.getLabel());
        navMenuItemHolder.iconView.setImageResource(menuItem.getIcon());
        
        return convertView ;
    }

    /**
     * 
     * @param convertView
     * @param parentView
     * @param navDrawerItem
     * @return
     */
    public View getSectionView(View convertView, ViewGroup parentView,
            NavDrawerItem navDrawerItem) {
        
        NavMenuSection menuSection = (NavMenuSection) navDrawerItem ;
        NavMenuSectionHolder navMenuItemHolder = null;
        
        if (convertView == null) {
            convertView = inflater.inflate( R.layout.nav_drawer_section, parentView, false);
            TextView labelView = (TextView) convertView
                    .findViewById( R.id.nav_menusection_label );

            navMenuItemHolder = new NavMenuSectionHolder();
            navMenuItemHolder.labelView = labelView ;
            convertView.setTag(navMenuItemHolder);
        }

        if ( navMenuItemHolder == null ) {
            navMenuItemHolder = (NavMenuSectionHolder) convertView.getTag();
        }
                    
        navMenuItemHolder.labelView.setText(menuSection.getLabel());
        
        return convertView ;
    }
    
    /*
     * (non-Javadoc)
     * @see android.widget.BaseAdapter#getViewTypeCount()
     */
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    /*
     * (non-Javadoc)
     * @see android.widget.BaseAdapter#getItemViewType(int)
     */
    @Override
    public int getItemViewType(int position) {
        return this.getItem(position).getType();
    }
    
    /*
     * (non-Javadoc)
     * @see android.widget.BaseAdapter#isEnabled(int)
     */
    @Override
    public boolean isEnabled(int position) {
        return getItem(position).isEnabled();
    }
    
    /**
     * 
     * @author Fernando C. de Souza
     * @version 1.0
     * @since 16/10/2013
     */
    private static class NavMenuItemHolder {
        private TextView labelView;
        private ImageView iconView;
    }
    
    /**
     * 
     * @author Fernando C. de Souza
     * @version 1.0
     * @since 16/10/2013
     */
    private class NavMenuSectionHolder {
        private TextView labelView;
    }
}
